define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Give, GoDialog, Auth, GoCatTypeAuth, $ionicModal) {

        $scope.scopeModel = new Object(); //数据模型
        $scope.scopeModel.paper_edit = new Object(); //和原数据里的区分--没有做

        $scope.datas = new Object();

        $scope.init_data = function() {
            Give.getlist('act_id', function(data) {
                $scope.scopeModel = data;
                $scope.is_show_goods();
            });
        }
        $scope.init_data(); //必须在定义之后

        //保存数据
        $scope.submit = function() {
            if($scope.input.cat3) {
                $scope.scopeModel.cat_type_id = $scope.input.cat1.id + "," + $scope.input.cat2.id + "," + $scope.input.cat3.id;
            } else if($scope.input.cat2) {
                $scope.scopeModel.cat_type_id = $scope.input.cat1.id + "," + $scope.input.cat2.id;
            } else if($scope.input.cat1) {
                $scope.scopeModel.cat_type_id = $scope.input.cat1.id;
            } else {
                $scope.scopeModel.cat_type_id = "";
            }
            if(!$scope.scopeModel.name) {
                GoDialog.toast("请输入活动名称！");
                return;
            } else if(!$scope.scopeModel.start_time) {
                GoDialog.toast("请输入活动的开始时间！");
                return;
            } else if(!$scope.scopeModel.apply_end_time) {
                GoDialog.toast("请输入活动的结束时间！");
                return;
            } else if(!$scope.scopeModel.end_time) {
                GoDialog.toast("请输入活动的开始时间！");
                return;
            } else if(!$scope.input.brand||$scope.input.brand.id==0) {
                GoDialog.toast("请选择品牌！");
                return;
            } else if(!$scope.scopeModel.cat_type_id) {
                GoDialog.toast("请选择分类！");
                return;
            } else if(!$scope.scopeModel.goods_name) {
                GoDialog.toast("请选择商品！");
                return;
            } else if(!$scope.scopeModel.price) {
                GoDialog.toast("请输入赠送价！");
                return;
            } else if(!$scope.scopeModel.count) {
                GoDialog.toast("请输入抽取数量！");
                return;
            } else {

                Give.edit($scope.scopeModel.act_id, $scope.scopeModel.name,$scope.scopeModel.abstract, $scope.scopeModel.start_time, $scope.scopeModel.end_time, $scope.scopeModel.apply_end_time, $scope.input.brand.id, $scope.scopeModel.cat_type_id, $scope.scopeModel.goods_id, $scope.scopeModel.price, $scope.scopeModel.count, $scope.scopeModel.paper_edit, function(data) {
                    if(data.success) {
                        GoDialog.toast("提交数据成功！");
                    } else {
                        GoDialog.toast("提交数据失败！");
                    }
                    //  console.log(data);
                });
            }

        }

        /***分类三级联动***/
        $scope.input = new Object();
        $scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
        $scope.input.brand = {id:0,name:"请选择品牌"};; //这是品牌下拉框的绑定数据
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
        //下拉侦听
        $scope.listen_brand = function(data, obj) {
            $scope.input.cat1 = {id:0,name:"请选择大分类"};
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        //定义绑定
        var items_cat = new Array();
        var obj_brand = {
            name: 'input.brand'
        };
        var obj1_cat = {
            name: 'input.cat1'
        };
        var obj2_cat = {
            name: 'input.cat2'
        };
        var obj3_cat = {
            name: 'input.cat3'
        };
        items_cat.push(obj_brand);
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load($scope, items_cat);
        $scope.doSelectBrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("品牌",$scope.brand_items,$scope.input.brand,function(data){
				$scope.input.brand = data;
			});
		}
        $scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
        //删除方法
        $scope.del = function() {
                $scope.scopeModel.goods_id = "";
                $scope.scopeModel.goods_name = "";
                $scope.scopeModel.mktprice = "";
                $scope.scopeModel.image = "";
                $scope.scopeModel.price = "";
                $scope.scopeModel.count = "";
                $scope.is_goods = false;

            }
            //删除答案
        $scope.delanswer = function(question_id, answer_id) {
            for(var k in $scope.scopeModel.paper.questions) {
                if($scope.scopeModel.paper.questions[k].id == question_id) {
                    for(var l in $scope.scopeModel.paper.questions[k].answer) {
                        if($scope.scopeModel.paper.questions[k].answer[l].id == answer_id) {
                            $scope.scopeModel.paper.questions[k].answer.splice(l, 1);
                        }
                    }
                }
            }
        }

        $ionicModal.fromTemplateUrl('templates/modal/searchpro_common.html', {
            scope: $scope,
            //          animation: 'slide-in-up'
        }).then(function(modal) {
            $scope.goodsModal = modal;
        });
        $scope.openGoods = function() {
            if($scope.scopeModel.goods_name) {
                GoDialog.toast("已经有商品存在，不能添加");
                return;
            }
            $scope.goodsModal.show();
        };
        $scope.closeGoods = function() {
            for(var i = 0; i < $scope.goods_data.goods.length; i++) {
                if($scope.goods_data.goods[i].checked == true) {
                    $scope.scopeModel.goods_id = $scope.goods_data.goods[i].goods_id;
                    $scope.scopeModel.goods_name = $scope.goods_data.goods[i].name;
                    $scope.scopeModel.mktprice = $scope.goods_data.goods[i].mktprice;
                    $scope.scopeModel.image = $scope.goods_data.goods[i].image.image
                    $scope.scopeModel.price = "";
                    $scope.scopeModel.count = "";
                    $scope.is_goods = true;
                    break;
                }
            }

            $scope.goodsModal.hide();
        };
        $scope.input.goods_count = 0;
        $scope.do_goodsSearch = function() {

            if(!$scope.input.brand) {
                $scope.input.brand = new Object();
                $scope.input.brand.id = "";
            } else {
                if($scope.input.cat3 && $scope.input.cat3.id != 0) {
                    $scope.cat_type_id = $scope.input.cat1.id + "," + $scope.input.cat2.id + "," + $scope.input.cat3.id;
                } else if($scope.input.cat2 && $scope.input.cat2.id != 0) {
                    $scope.cat_type_id = $scope.input.cat1.id + "," + $scope.input.cat2.id;
                } else if($scope.input.cat1 && $scope.input.cat1.id != 0) {
                    $scope.cat_type_id = $scope.input.cat1.id;
                } else {
                    $scope.input.cat_type_id = "";
                }
            }
            Give.goods_list($scope.input.cat_type_id, $scope.input.name, $scope.input.brand.id, function(data) {
                $scope.goods_data = data;
                $scope.input.goods_count = $scope.goods_data.page[0].count;
            })

        }

        //判断是否有商品
        $scope.is_show_goods = function() {
            console.log($scope.scopeModel.goods_name);
            if($scope.scopeModel.goods_name != undefined) {
                $scope.is_goods = true;
            } else {
                $scope.is_goods = false;
            }
        }

        $scope.goods_checked = function(goods_id) {
            for(var i = 0; i < $scope.goods_data.goods.length; i++) {
                if($scope.goods_data.goods[i].goods_id == goods_id) {
                    $scope.goods_data.goods[i].checked = true;
                } else
                    $scope.goods_data.goods[i].checked = false;

            }
            // console.log($scope.goods_data);
        }
        //取消
        $scope.tui=function(){
            window.history.go(-1);
        }

    }

    controller.$inject = ['$scope', 'Give', 'GoDialog', 'Auth', 'GoCatTypeAuth', '$ionicModal'];

    return app.registerController("givectrl",controller);
});